package com.csxh.web.service.eshop;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.csxh.web.dao.IDao;
import com.csxh.web.dao.eshop.ProductDao;
import com.csxh.web.mapper.eshop.ProductMapper;
import com.csxh.web.model.eshop.Product;
import com.csxh.web.model.eshop.ProductCondition;
import com.csxh.web.service.PagerService;

@Service
public class ProductService extends PagerService<Product> {

	private ProductDao thisDao;

	@Autowired
	public void setDao(IDao<Product> dao) {
		this.thisDao = (ProductDao) dao;
		super.setDao(dao);
	}

	public List<Product> findTodayHotList() {
		SqlSession session = this.thisDao.getSessionFactory().openSession();

		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id", "name");
		condition.setOrderField("visitCount");
		condition.setOffset(0);
		condition.setLimit(10);
		List<Product> list = mapper.selectAllByCondition(condition);
		return list;
	}

	public List<Product> findBestSellList() {

		SqlSession session = this.thisDao.getSessionFactory().openSession();

		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id", "name");
		condition.setOrderField("sellCount");
		condition.setOffset(0);
		condition.setLimit(10);
		List<Product> list = mapper.selectAllByCondition(condition);
		return list;
	}

	public Product findNew() {

		SqlSession session = this.thisDao.getSessionFactory().openSession();
		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id", "name","author","smallImg","description");
		condition.setWhere(" `addDate` = ( SELECT MAX( `addDate` ) FROM product  )  ");
		Product product = mapper.selectOneByCondition(condition);
		return product;
	}

	public Product findCommend() {
		SqlSession session = this.thisDao.getSessionFactory().openSession();
		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id", "name","smallImg","description");
		condition.setWhere(" `commend` = 1 ");
		Product product = mapper.selectOneByCondition(condition);
		return product;
	}

	public Product findHotDeal() {
		SqlSession session = this.thisDao.getSessionFactory().openSession();
		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id", "name","smallImg","description","price","listPrice");
		condition.setWhere(" `hotDeal` = 1 ");
		Product product = mapper.selectOneByCondition(condition);
		return product;
	}

	public Product findOneById(int id) {
		
		SqlSession session = this.thisDao.getSessionFactory().openSession();
		ProductMapper mapper = session.getMapper(ProductMapper.class);
		ProductCondition condition = new ProductCondition();
		condition.setFieldNames("id,isbn,supplier,name,author,description,price,hotDeal,listPrice,pubDate,bigImg,unitWeight");
		condition.setWhere(" `id` ="+id);
		Product product = mapper.selectOne(condition);
		return product;
	}

}
